
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Atienza-Home
 */
public class InitMain2 extends javax.swing.JFrame implements Runnable{
    
    String[] fruits = {"Apple","Guyabano","Lemon","Mango","Melon","Orange","Pear","Pineapple","Strawberry","Watermelon"};
    ImageIcon basket = new ImageIcon("resources\\basket.png");
    ArrayList<String> listz1 = new ArrayList<String>();
    ArrayList<String> listz2 = new ArrayList<String>();
    String[] list1;
    String[] list2 = new String[0];
    Thread thread;
    DistribFrame2 dframe;
    boolean isdframe=false;
    
    
    //FOR buttons of fruit
    ArrayList<JButton> fbutton1 = new ArrayList<JButton>();
    ArrayList<JButton> fbutton2 = new ArrayList<JButton>();
    ArrayList<JButton> fbutton3 = new ArrayList<JButton>();
    ArrayList<JPanel>  fpanel = new ArrayList<JPanel>(); 
    //////////////////////////////   
    
    //FOR buttons of basket
    ArrayList<JButton> baskets = new ArrayList<JButton>();
    
    
    ///////////////////
    
   
    ArrayList<Fruit> arrfruits = new ArrayList<Fruit>();
    boolean isclickedonce=false;
    /**
     * Creates new form InitMain2
     */
    public InitMain2() {
        
        setVisible(true);
        setResizable(false);
        JButton button;
        
        for(int i=0;i<fruits.length;i++){
            Fruit fruit = new Fruit();
            fruit.setName(fruits[i]);
            fruit.setQuantity(0);
            fruit.setIndex(i);
            arrfruits.add(fruit);
            
           
        }
        
        for(int i=0;i<fruits.length;i++){
            listz1.add(fruits[i]);
        }
        list1=new String[listz1.size()];
        list1=passArrayListtoString(list1,listz1);
        
              
        
        initComponents();
        for(int i=0;i<fruits.length;i++){
            JPanel panel = new JPanel();
            panel = new JPanel();
            panel.setLayout(new GridLayout(3,1));
            button = new JButton();
            button.setEnabled(false);
            fbutton1.add(button);
            panel.add(fbutton1.get(i));
            button = new JButton(arrfruits.get(i).getName());
            fbutton2.add(button);
            panel.add(fbutton2.get(i));
            button = new JButton("0"); 
            fbutton3.add(button);
            panel.add(fbutton3.get(i));
            fpanel.add(panel);
            jPanel3.add(fpanel.get(i));
            fpanel.get(i).setVisible(false);
            jScrollPane2.setViewportView(jPanel3);
            
            
            button = new JButton(basket);
            baskets.add(button);
            baskets.get(i).setVisible(false);
            jPanel4.add(baskets.get(i));
            jScrollPane4.setViewportView(jPanel4);
        }
        
       
        for(int i=0;i<baskets.size();i++){
            baskets.get(i).addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    for(int j=baskets.size()-1;j>=0;j--){
                        if(baskets.get(j).isVisible()==true && isdframe==false){
                            baskets.get(j).setVisible(false);
                            break;
                        }
                        
                    }
                }
                
            });
            
            
        }
        
        for(int i=0;i<fbutton2.size();i++){
            final int t = i;
            
            fbutton2.get(i).addActionListener(new ActionListener(){
                 
                public void actionPerformed(ActionEvent e) {
                    if(isdframe==false){
                        arrfruits.get(t).setQuantity(arrfruits.get(t).getQuantity()+1);
                        fbutton3.get(t).setText(String.valueOf(arrfruits.get(t).getQuantity()));
                    }
                    
                }
                
            });
            
            
        }
        
        
        for(int i=0;i<fbutton3.size();i++){
            final int t = i;
            try{if(dframe.isframeenable==true) break;}catch(Exception ex){}
            fbutton3.get(i).addActionListener(new ActionListener(){
                 
                public void actionPerformed(ActionEvent e) {
                    if(fbutton3.get(t).getText().equals("1")==false){
                        if(isdframe==false){
                            arrfruits.get(t).setQuantity(arrfruits.get(t).getQuantity()-1);
                            fbutton3.get(t).setText(String.valueOf(arrfruits.get(t).getQuantity()));
                        }
                    }
                }
                
            });
            
            
        }
        
        
     
        thread = new Thread(this);
        thread.start();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList(list1);
        jLabel2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList(list2);
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel1.setText("Add a fruit:");

        
        jScrollPane1.setViewportView(jList1);

        jLabel2.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel2.setText("Remove a fruit:");

       
        jScrollPane3.setViewportView(jList3);

        jButton2.setText("Add a Basket");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Proceed>>");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel3.setPreferredSize(new java.awt.Dimension(635, 97));
        jPanel3.setLayout(new GridLayout(1,10));
            
        
        jScrollPane2.setBorder(null);
        jScrollPane2.setViewportView(jPanel3);
        

        jPanel4.setPreferredSize(new java.awt.Dimension(635, 97));
        jPanel4.setLayout(new GridLayout(1,10));
        
        
        
        jScrollPane4.setBorder(null);
        jScrollPane4.setViewportView(jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 198, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jScrollPane2)
            .addComponent(jScrollPane4)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        for(int i=0;i<baskets.size();i++){
            if(baskets.get(i).isVisible()==false){
                baskets.get(i).setVisible(true);
                break;
            }
        }
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        int fruitcount=0,basketcount=0,totalfruits=0;
        int tint=0,tindex=0; boolean ismaxrepeat=false;
        
        for(int i=0;i<arrfruits.size();i++){
            if(fpanel.get(i).isVisible()==true)
                fruitcount++;
            if(baskets.get(i).isVisible()==true)
                basketcount++;
            
            
            totalfruits+=arrfruits.get(i).getQuantity();
            
        }
        
        for(int i=0;i<arrfruits.size();i++){
            if(tint<arrfruits.get(i).getQuantity()){
                tint = arrfruits.get(i).getQuantity();
                tindex = i;
            }
        }
        
        for(int i=0;i<arrfruits.size();i++){
            if(tint==arrfruits.get(i).getQuantity() && tindex!=i)
                ismaxrepeat=true;
            
        }
       
        if(fruitcount<2 && basketcount<3)
           JOptionPane.showMessageDialog(null,"Please add more fruits (min. 2) and baskets (min.3).");
        else if(fruitcount<2)
            JOptionPane.showMessageDialog(null,"Please add more fruits (min. 2).");
        else if(basketcount<3)
            JOptionPane.showMessageDialog(null,"Please add more baskets (min. 3).");
        else if(totalfruits<basketcount)
            JOptionPane.showMessageDialog(null,"Fruits are less than the baskets!");
        else if(ismaxrepeat==true)
            JOptionPane.showMessageDialog(null,"Max fruits are repeated!");
        else{
            setVisible(false);
            dframe = new DistribFrame2(arrfruits,fbutton1,fbutton2,fbutton3,baskets,fpanel);
            isdframe=true;
        }
        
        
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JList jList3;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    // End of variables declaration

    @Override
    public void run() {
        int t=0;
        String temp;
        JButton button;
        JPanel panel;
        
        while(true){
            
           try{
           if(jList1.getSelectedIndex()!=-1){
              
               listz2.add(listz1.get(jList1.getSelectedIndex())); 
               for(int i=0;i<fpanel.size();i++){
                   if(listz1.get(jList1.getSelectedIndex()).equals(fbutton2.get(i).getText())){
                       fpanel.get(i).setVisible(true);
                       arrfruits.get(i).setQuantity(arrfruits.get(i).getQuantity()+1);
                       fbutton3.get(i).setText(String.valueOf(arrfruits.get(i).getQuantity()));
                   }
               }
               list2 = new String[listz2.size()];
               list1 = passArrayListtoString(list2,listz2);
               jList3.setModel(new javax.swing.AbstractListModel() {
                   public int getSize() { return list2.length; }
                   public Object getElementAt(int i) { return list2[i];}
               });
                             
               
               
               
               
               
               
               
               
               
               listz1.remove(jList1.getSelectedIndex());
               list1 = new String[listz1.size()];
               list1 = passArrayListtoString(list1,listz1);
                
               jList1.setModel(new javax.swing.AbstractListModel() {
                   public int getSize() { return list1.length; }
                   public Object getElementAt(int i) { return list1[i];}
               });
                
                              
                
           }
           
           
           if(jList3.getSelectedIndex()!=-1 ){
           
              
               listz1.add(listz2.get(jList3.getSelectedIndex())); 
               String[] tl = new String[listz1.size()];
               for(int i=0;i<listz1.size();i++){
                    tl[i] = listz1.get(i);            
               }
        
               Arrays.sort(tl);
               listz1 = new ArrayList<String>();
               for(int i=0;i<tl.length;i++){
                   listz1.add(tl[i]);
                   
               }
               
               for(int i=0;i<fpanel.size();i++){
                   if(listz2.get(jList3.getSelectedIndex()).equals(fbutton2.get(i).getText())){
                     
                       fpanel.get(i).setVisible(false);
                       arrfruits.get(i).setQuantity(0);
                       fbutton3.get(i).setText(String.valueOf(arrfruits.get(i).getQuantity()));
                       break;
                       
                   }
               }
               
               list1 = new String[listz1.size()];
               list2 = passArrayListtoString(list1,listz1);
               jList1.setModel(new javax.swing.AbstractListModel() {
                   public int getSize() { return list1.length; }
                   public Object getElementAt(int i) { return list1[i];}
               });
               
               
            
              
               
               listz2.remove(jList3.getSelectedIndex());
               list2 = new String[listz2.size()];
               list2 = passArrayListtoString(list2,listz2);
                
               jList3.setModel(new javax.swing.AbstractListModel() {
                   public int getSize() { return list2.length; }
                   public Object getElementAt(int i) { return list2[i];}
               });
                
                              
               
           }
           
           
           
           }catch(Exception e){}
          
            
        }
    }
    
    public String[] passArrayListtoString(String[] temp,ArrayList<String> str){
        
        for(int i=0;i<temp.length;i++){
            temp[i] = str.get(i);            
        }
        
        Arrays.sort(temp);
        
        return temp;
        
    }

    
}
